<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>防抖</title>
</head>
<body>
  <input id="input" type="text">
  <script>
    function debounce(fn, delay, immediate ){
      let timer = null
      return function (){
        
        timer && clearTimeout(timer)

        if(immediate) {
          if(!timer) {
            fn.call(this)
          }
          timer = setTimeout(() => {
            timer = null
          }, delay);
        } else {
          timer = setTimeout(() => {
            fn.call(this)
          }, delay);
        }
      }
    }
  </script>
</body>
</html>